package com.asa.tongji;

import java.math.RoundingMode;
import java.text.NumberFormat;

public class FenBuFunction {
	/**
	 * 根据置信度求标准正态分布的a值
	 */
	
	public static double zhengtai(double a){
		double ss = a/2;
		double buchang = 0.001;
		double x = 0;
		double ss2 = 0;
		while(true){
			double n1 = BaseFunction.normaldistribution(x, 0, 1);
			x=x+buchang;
			double n2 = BaseFunction.normaldistribution(x, 0, 1);
			ss2 += (n1+n2)*buchang/2;
			if (ss2>=ss) {
				break;
			}
		}
		NumberFormat nf = NumberFormat.getInstance();
        nf.setRoundingMode(RoundingMode.HALF_UP);//设置四舍五入
        nf.setMinimumFractionDigits(6);//设置最小保留几位小数
		
		return Double.parseDouble(nf.format(x));
	}
	
	
	/**
	 * 根据置信度求和样本大小求t分布的t值
	 * @param a	置信度
	 * @param n	样本大小
	 * @return	返回值
	 */
	public static double tfengbu(double a,int n){
		double ss = a/2;
		double buchang = 0.0001;
		double x = 0;
		double ss2 = 0;
		double one = BaseFunction.gamma((double)(n+1)/2);
		double two = BaseFunction.gamma((double)n/2);
		while(true){
			double n1 = BaseFunction.tfengbu(x, n,one,two);
			x=x+buchang;
			double n2 = BaseFunction.tfengbu(x, n,one,two);
			ss2 = ss2 + (n1+n2)*buchang/2;
			////System.out.println(ss+"================="+ss2+"++++++++++++++++++++++++++"+(n1+n2)*buchang/2);
			if (ss2>=ss) {
				break;
			}
		}
		NumberFormat nf = NumberFormat.getInstance();
        nf.setRoundingMode(RoundingMode.HALF_UP);//设置四舍五入
        nf.setMinimumFractionDigits(6);//设置最小保留几位小数
		
		return Double.parseDouble(nf.format(x));
	}
	public static double tfengbu(double a,double n){
		double ss = a/2;
		double buchang = 0.0001;
		double x = 0;
		double ss2 = 0;
		double one = BaseFunction.gamma((double)(n+1)/2);
		double two = BaseFunction.gamma((double)n/2);
		while(true){
			double n1 = BaseFunction.tfengbu(x, n,one,two);
			x=x+buchang;
			double n2 = BaseFunction.tfengbu(x, n,one,two);
			ss2 = ss2 + (n1+n2)*buchang/2;
			////System.out.println(ss+"================="+ss2+"++++++++++++++++++++++++++"+(n1+n2)*buchang/2);
			if (ss2>=ss) {
				break;
			}
		}
		NumberFormat nf = NumberFormat.getInstance();
        nf.setRoundingMode(RoundingMode.HALF_UP);//设置四舍五入
        nf.setMinimumFractionDigits(6);//设置最小保留几位小数
		
		return Double.parseDouble(nf.format(x));
	}
	
	
	
	
	
	public static double[] kafangfengbu(double a,int n) throws Exception{
		n = n-1;
		if (a>0.5) {
			a = 1 - a;
		}
		a = a/2;
		double ss = a;
		double buchang = 0.001;
		double x = 0.001;
		double ss2 = 0;
		double one = BaseFunction.gamma((double)(n)/2);
		while (true) {
			double n1 = BaseFunction.kafangfengbu(x, n,one);
			x=x+buchang;
			double n2 = BaseFunction.kafangfengbu(x, n,one);
			ss2 = ss2 + (n1+n2)*buchang/2;
			if (ss2>=ss) {
				break;
			}
		}
		double[] asa = new double[2];
		asa[0] = x;
		ss = 1 - a;
		while (true) {
			double n1 = BaseFunction.kafangfengbu(x, n,one);
			x=x+buchang;
			double n2 = BaseFunction.kafangfengbu(x, n,one);
			ss2 = ss2 + (n1+n2)*buchang/2;
			if (ss2>=ss) {
				break;
			}
		}
		asa[1] = x;
		return asa;
	}
	
	/**
	 * 不知道为什么，大于20的拟合不怎么好使，n1与n2差值太大也拟合不怎么好
	 * @param a
	 * @param n1
	 * @param n2
	 * @return
	 * @throws Exception
	 */
	public static double[] ffengbu(double a,int n1,int n2) throws Exception{
		if (n1>120&&n2>120) {
			double[] asa = {1.0,1.0};
			return asa;
		}
		if (a<0.5) {
			a = 1 - a;
		}
		double ss = a;
		double buchang = 0.001;
		double x = 0.001;
		double ss2 = 0;
		
		double one;
		double two;
		double three;
		if ((n1+n2)>60) {
			if (n1>n2) {
				one = 1;
				for (int i = n1/2+1; i <= (n1+n2)/2+1; i++) {
					one = one*i;
				}
				two = 1;
				three = BaseFunction.gamma((double)n2/2);
			}else{
				one = 1;
				for (int i = n2/2+1; i <= (n1+n2)/2+1; i++) {
					one = one*i;
				}
				two = 1;
				three = BaseFunction.gamma((double)n1/2);
			}
		}else{
			one = BaseFunction.gamma((double)(n1+n2)/2);
			two = BaseFunction.gamma((double)n1/2);
			three = BaseFunction.gamma((double)n2/2);
		}
		
	
		//System.out.println(one+"......................"+two+"           "+three);
		while (true) {
			double d1 = BaseFunction.ffengbu(n1, n2, x, one, two, three);
			x=x+buchang;
			
			double d2 = BaseFunction.ffengbu(n1, n2, x, one, two, three);
			//System.out.println(d1+".............."+d2);
			ss2 = ss2 + (d1+d2)*buchang/2;
			//System.out.println(ss2+"          "+(d1+d2)*buchang/2);
			if (d1+d2==0) {
				buchang = buchang*10;
				if (buchang>1) {
					break;
				}
			}
			if (ss2>=ss) {
				break;
			}
		}
		
		double[] asa = new double[2];
		if (x<1) {
			asa[0] = x;
			asa[1] = 1/x;
		}else{
			asa[1] = x;
			asa[0] = 1/x;
		}
		
		return asa;
	}
	
	
	
	public static void main(String[] args) throws Exception {
		double[] ffengbu = ffengbu(0.05, 1, 12);
		for (int i = 0; i < ffengbu.length; i++) {
			//System.out.println(ffengbu[i]);
		}
		
		
		
		//System.out.println(BaseFunction.gamma(1));
		//System.out.println(BaseFunction.gamma(6));
		//System.out.println(BaseFunction.gamma(5.999));
	}
	
	
	
	
}
